package com.abel.amq.consumer.service.impl;

import com.abel.amq.spring.entity.MailContent;
import com.abel.amq.consumer.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

/**
 * @author abel.he
 * @data 2018/8/8 16:49
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService {

    @Autowired
    JavaMailSenderImpl sender;

    @Autowired
    SimpleMailMessage mailMessage;

    @Autowired
    ThreadPoolTaskExecutor pool;


    @Override
    public void sendMail(final MailContent mailContent) {
        System.out.println(mailContent);

        this.pool.execute(new Runnable() {
            @Override
            public void run() {
                mailMessage.setTo(mailContent.getTo());
                mailMessage.setSubject(mailContent.getSubject());
                mailMessage.setText(mailContent.getContent());
                sender.send(mailMessage);
            }
        });
    }
}
